package com.sms.client.ui;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.sms.client.ServiceUtil;
import com.sms.client.utils.LoadingBox;
import com.sms.shared.SessionInfo;

public class LiveProductionUI extends Composite {
	private Long siteId;
	private String groupNo;
	private FlexTable todayHeading;
	private FlexTable dataFlowValue;
	private FlowPanel flowPanel;
	private FlexTable todayDataTable;
	private FlexTable solarTable;
	private FlexTable greenHouseTable;
	private String inverterType;
	private Timer timer; 
	private String siteName;
	private FlexTable dataFlowHeading;
	private FlexTable dataFlowTable;
	private FlexTable dgRunningTable;
	private String partyId;
	public LiveProductionUI(Long siteId, String siteName,String groupNo) {
		this.siteId = siteId;
		this.siteName = siteName;
		this.groupNo = groupNo;
		initUI();
	}

	private void initUI() {
		SessionInfo userSessionInfo = SessionInfo.getSharedInstance();
		final String sessionId = (String) userSessionInfo.getSessionData(SessionInfo.JSESSION_ID);
		inverterType = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_INVERTERTYPE);
		partyId = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_PARTYID);
		System.err.println(" sessionid : " + sessionId);
		final FlexTable mainTable = new FlexTable();
		mainTable.setWidth("100%");
		final FlexTable dataFlowMain = new FlexTable();
		greenHouseTable = getLiveTable("greenhouse", "15528", "28", "28");
		dataFlowMain.setWidget(0, 0, greenHouseTable);
		solarTable = getLiveTable("solar", "2", "06:52:06AM", "06:58:44PM");
		if(inverterType.equalsIgnoreCase("Off-Grid With DG")){
			dgRunningTable = getDGRunningTable();
			dataFlowMain.setWidget(1, 0, dgRunningTable);
			dataFlowMain.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
			populateRunningHours();
		}
		
		dataFlowValue = new FlexTable();
		flowPanel = new FlowPanel();
		dataFlowHeading = new FlexTable();
		dataFlowHeading.setStyleName("box_header1");
		dataFlowHeading.setWidget(0, 0, new HTML("System size for solar panel : "));
		dataFlowHeading.setWidget(0, 1, new HTML());
		dataFlowHeading.setWidget(0, 2, new HTML("Inverter size : "));
		dataFlowHeading.setWidget(0, 3, new HTML());
		dataFlowHeading.setWidget(0, 4, new HTML("Battery bank capacity : "));
		dataFlowHeading.setWidget(0, 5, new HTML());
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 0, "body_txt_blue2_b");
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 2, "body_txt_blue2_b");
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 4, "body_txt_blue2_b");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 0, "23%");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 1, "12%");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 2, "19%");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 3, "10%");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 4, "26%");
		dataFlowHeading.getFlexCellFormatter().setWidth(0, 5, "10%");
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 1, "body_txt_black_b");
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 3, "body_txt_black_b");
		dataFlowHeading.getFlexCellFormatter().setStyleName(0, 5, "body_txt_black_b");
		
		dataFlowHeading.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		dataFlowHeading.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_RIGHT);
		dataFlowHeading.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_RIGHT);

		flowPanel.add(dataFlowValue);
		dataFlowHeading.setWidth("100%");
		dataFlowTable = new FlexTable();
		dataFlowTable.setWidth("100%");
		dataFlowTable.setWidget(0, 0, dataFlowHeading);
		dataFlowTable.setWidget(1, 0, flowPanel);
		dataFlowMain.setWidget(0, 1, dataFlowTable);
		dataFlowMain.getFlexCellFormatter().setRowSpan(0, 1, 2);
		dataFlowMain.setWidget(0, 2, solarTable);

		dataFlowMain.getFlexCellFormatter().setWidth(0, 0, "20%");
		dataFlowMain.getFlexCellFormatter().setWidth(0, 1, "70%");
		dataFlowMain.getFlexCellFormatter().setWidth(0, 2, "10%");
		dataFlowMain.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		dataFlowMain.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		dataFlowMain.getFlexCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_TOP);
		String roleName = (String) userSessionInfo.getSessionData(SessionInfo.SESSION_ROLENAME);
		Long mPartyId = Long.valueOf(partyId);
		if (roleName.equalsIgnoreCase("Admin"))
			mPartyId = null;
		ServiceUtil.getMeterServiceAsync().getSites(mPartyId, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(Object result) {
				List<ArrayList> dataList = (List) result;
				List<String> locationList = new ArrayList<String>();
				System.err.println("dataList : "+dataList.toString() + dataList.size());
				if (dataList.size() > 1) {
					TreeItem rootItem = new TreeItem("Location");
					for (ArrayList list : dataList) {
						if (!locationList.contains((String) list.get(2)))
							locationList.add((String) list.get(2));
					}
					for (String location : locationList) {
						final TreeItem locationItem = new TreeItem(location);
						List<ArrayList> siteDataList = getSiteGroupNo(location, dataList);

						for (ArrayList dataValueList : siteDataList) {
							TreeItem groupNoTree = new TreeItem((String) dataValueList.get(1));
							groupNoTree.setUserObject(dataValueList);
							locationItem.addItem(groupNoTree);
						}
						rootItem.addItem(locationItem);
					}
					Tree tree = new Tree();
					tree.addItem(rootItem);
					mainTable.setWidget(0, 0, tree);
					mainTable.getFlexCellFormatter().setWidth(0, 0, "10%");
					mainTable.getFlexCellFormatter().setRowSpan(0, 0, 2);
					mainTable.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
					mainTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);
					tree.addSelectionHandler(new SelectionHandler<TreeItem>() {

						@Override
						public void onSelection(SelectionEvent<TreeItem> event) {
							if (event.getSelectedItem().getUserObject() != null) {
								ArrayList dataValueList = (ArrayList) event.getSelectedItem().getUserObject();
								siteId = (Long) dataValueList.get(0);
								groupNo = (String) dataValueList.get(1);
								siteName = (String) dataValueList.get(3);
								inverterType = (String) dataValueList.get(4);
								todayHeading.setWidget(0, 1, new HTML(siteName));
								todayHeading.setWidget(0, 2, new HTML("("+groupNo+")"));
								
								if(inverterType.equalsIgnoreCase("Off-Grid With DG")){
									dgRunningTable = getDGRunningTable();
									dataFlowMain.setWidget(1, 0, dgRunningTable);
									dataFlowMain.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
									populateRunningHours();
								}else{
									dataFlowMain.setWidget(1, 0,new HTML());
								}

								ServiceUtil.getUserLoginServiceAsync().setSessionData(groupNo, inverterType,sessionId, new AsyncCallback() {

									@Override
									public void onFailure(Throwable caught) {
										caught.printStackTrace();
									}

									@Override
									public void onSuccess(Object result) {
										if (result == null)
											return;
										Map resultMap = (Map) result;
										SessionInfo mUserSessionInfo = (SessionInfo) resultMap.get("result");
										SessionInfo.setUserSessionInfo(mUserSessionInfo);

									}
								});
								populateData();
							}
						}
					});
				}else{
					mainTable.setWidget(0, 0, new HTML(""));
					mainTable.getFlexCellFormatter().setWidth(0, 0, "10%");
					mainTable.getFlexCellFormatter().setRowSpan(0, 0, 2);
				}

			}
		});

		FlexTable todayMain = new FlexTable();
		todayMain.setWidth("100%");

		todayDataTable = new FlexTable();
		todayDataTable.setWidget(0, 0, getTodayTable("solar", "0.0 kWh"));
		todayDataTable.setWidget(0, 1, new HTML());
		todayDataTable.setWidget(0, 2, getTodayTable("facility", "0.0 kWh"));
		todayDataTable.setWidget(0, 3, new HTML());
		todayDataTable.setWidget(0, 4, getTodayTable("import", "0.0 kWh"));
		todayDataTable.setWidget(0, 5, new HTML());
		todayDataTable.setWidget(0, 6, getTodayTable("export", "0.0 kWh"));
		todayDataTable.setWidth("100%");
		// ftTodayUssage.setStyleName("box_bg");
		// ftTodayUssage.setHeight("120px");
		todayDataTable.getFlexCellFormatter().setWidth(0, 0, "23%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 1, "2%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 2, "23%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 3, "2%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 4, "23%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 5, "2%");
		todayDataTable.getFlexCellFormatter().setWidth(0, 6, "25%");
		todayDataTable.getFlexCellFormatter().setStyleName(0, 1, "table_in");
		todayDataTable.getFlexCellFormatter().setStyleName(0, 3, "table_in");
		todayDataTable.getFlexCellFormatter().setStyleName(0, 5, "table_in");
		todayDataTable.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		todayDataTable.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_CENTER);
		todayDataTable.getFlexCellFormatter().setHorizontalAlignment(0, 4, HasHorizontalAlignment.ALIGN_CENTER);
		todayDataTable.getFlexCellFormatter().setHorizontalAlignment(0, 6, HasHorizontalAlignment.ALIGN_CENTER);
		todayDataTable.setStyleName("box_main_s");
		todayHeading = new FlexTable();
		todayHeading.setWidget(0, 0, new HTML("Today"));
		todayHeading.setWidth("100%");
		String todayDate = DateTimeFormat.getFormat("EEEE; MMM dd, yyyy").format(new Date());
		String[] todayDateArray = todayDate.split(";");
		todayHeading.setWidget(0, 1, new HTML(siteName));
		todayHeading.setWidget(0, 2, new HTML("("+groupNo+")"));
		todayHeading.setWidget(0, 3, new HTML(todayDateArray[0]));
		todayHeading.setWidget(0, 4, new HTML(todayDateArray[1]));
		todayHeading.getFlexCellFormatter().setWidth(0, 0, "5%");
		todayHeading.getFlexCellFormatter().setWidth(0, 1, "40%");
		todayHeading.getFlexCellFormatter().setWidth(0, 2, "35%");
		todayHeading.getFlexCellFormatter().setWidth(0, 3, "10%");
		todayHeading.getFlexCellFormatter().setWidth(0, 4, "11%");
		todayHeading.getFlexCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_RIGHT);
		todayHeading.getFlexCellFormatter().setHorizontalAlignment(0, 2, HasHorizontalAlignment.ALIGN_LEFT);
		todayHeading.getFlexCellFormatter().setHorizontalAlignment(0, 3, HasHorizontalAlignment.ALIGN_RIGHT);

		todayHeading.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
		todayHeading.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_MIDDLE);
		todayHeading.getFlexCellFormatter().setVerticalAlignment(0, 2, HasVerticalAlignment.ALIGN_MIDDLE);
		todayHeading.getFlexCellFormatter().setVerticalAlignment(0, 3, HasVerticalAlignment.ALIGN_MIDDLE);
		todayHeading.getFlexCellFormatter().setVerticalAlignment(0, 4, HasVerticalAlignment.ALIGN_MIDDLE);

		todayHeading.getFlexCellFormatter().setStyleName(0, 0, "value_blue_m");
		todayHeading.getFlexCellFormatter().setStyleName(0, 1, "value_blue_s");
		todayHeading.getFlexCellFormatter().setStyleName(0, 2, "body_txt_black_b");
		todayHeading.getFlexCellFormatter().setStyleName(0, 3, "value_blue_s");
		todayHeading.getFlexCellFormatter().setStyleName(0, 4, "body_txt_black_b");
		todayHeading.setStyleName("box_header1");
		todayMain.setWidget(0, 0, todayHeading);
		todayMain.setWidget(1, 0, todayDataTable);

		mainTable.setWidget(0, 1, todayMain);
		mainTable.setWidget(1, 0, dataFlowMain);
		mainTable.getFlexCellFormatter().setWidth(0, 1, "90%");
		mainTable.getFlexCellFormatter().setWidth(1, 0, "90%");
		mainTable.getFlexCellFormatter().setHeight(0, 1, "33%");
		mainTable.getFlexCellFormatter().setHeight(1, 0, "67%");
		mainTable.getFlexCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
		mainTable.getFlexCellFormatter().setStyleName(0, 1, "box_flow_bg");
		//dataFlowValue.setBorderWidth(1);
		dataFlowValue.setWidth("100%");
		dataFlowValue.setHeight("100%");
		System.err.println("screen height :"+getScreenHeight());
		dataFlowValue.setHeight((getScreenHeight()*43)/100+"px");
		dataFlowValue.setWidget(0, 0, new HTML());
		dataFlowValue.setWidget(0, 1, new HTML());
		dataFlowValue.setWidget(0, 2, new HTML());
		dataFlowValue.setWidget(0, 3, new HTML());
		
		dataFlowValue.setWidget(1, 0, new HTML());
		dataFlowValue.setWidget(1, 1, new HTML());
		dataFlowValue.setWidget(1, 2, new HTML());
		dataFlowValue.setWidget(1, 3, new HTML());

		dataFlowValue.setWidget(2, 0, new HTML());
		dataFlowValue.setWidget(2, 1, new HTML());
		dataFlowValue.setWidget(2, 2, new HTML());
		dataFlowValue.setWidget(2, 3, new HTML());

		dataFlowValue.setWidget(3, 0, new HTML());
		dataFlowValue.setWidget(3, 1, new HTML());
		dataFlowValue.setWidget(3, 2, new HTML());
		dataFlowValue.setWidget(3, 3, new HTML());
		
		dataFlowValue.getFlexCellFormatter().setWidth(0, 0, "36%");
		dataFlowValue.getFlexCellFormatter().setWidth(0, 1, "10%");
		dataFlowValue.getFlexCellFormatter().setWidth(0, 2, "18%");
		dataFlowValue.getFlexCellFormatter().setWidth(0, 3, "36%");

		dataFlowValue.getFlexCellFormatter().setHeight(0, 0, "5%");
		dataFlowValue.getFlexCellFormatter().setHeight(1, 0, "34%");
		dataFlowValue.getFlexCellFormatter().setHeight(2, 0, "17%");
		dataFlowValue.getFlexCellFormatter().setHeight(3, 0, "44%");
		initWidget(mainTable);
		refreshPage();
		populateData();

	}
	

private void refreshPage() {
    timer = new Timer() {
       public void run() {
    	  populateData();
    	  refreshPage();
      }
    };
    // Schedule the timer to run once in 5 minutes.
    timer.schedule(5*60*1000);
}


	private List getSiteGroupNo(String location, List<ArrayList> siteList) {
		List dataList = new ArrayList();
		for (ArrayList list : siteList) {
			if (((String) list.get(2)).equalsIgnoreCase(location)) {
				dataList.add(list);
			}

		}
		return dataList;
	}

	private void populateData() {
		LoadingBox.getSharedInstance().show();
		ServiceUtil.getMeterServiceAsync().getSiteLiveData(siteId, groupNo, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				LoadingBox.getSharedInstance().hide();
				// TODO Auto-generated method stub

			}

			@Override
			public void onSuccess(Object result) {
				Map dataMap = (Map) result;
				Set set = dataMap.entrySet();
				Iterator i = set.iterator();
				int rowIndex = 0;
				while (i.hasNext()) {
					Map.Entry me = (Map.Entry) i.next();
					inverterType = (String) me.getKey();
					String existingStyle = flowPanel.getStyleName();
					if (existingStyle.equalsIgnoreCase("offGrid") || existingStyle.equalsIgnoreCase("offGrid_WithoutUtility") || existingStyle.equalsIgnoreCase("onGrid_ToUtility") 
							|| existingStyle.equalsIgnoreCase("onGrid_FromUtility")
							|| existingStyle.equalsIgnoreCase("offGrid_WithDG")
							)
						flowPanel.removeStyleName(existingStyle);
					if (inverterType.equalsIgnoreCase("Off-Grid"))
						flowPanel.setStyleName("offGrid");
					if (inverterType.equalsIgnoreCase("Off-Grid Without Utility"))
						flowPanel.setStyleName("offGrid_WithoutUtility");
					if (inverterType.equalsIgnoreCase("Off-Grid With DG"))
						flowPanel.setStyleName("offGrid_WithDG");

					FlexTable valueTable = (FlexTable) solarTable.getWidget(1, 0);
					String todayDate = DateTimeFormat.getFormat("EEEE; MMM dd, yyyy hh:mm a").format(new Date());
					String[] todayDateArray = todayDate.split(";");
					HTML todayDateHTML = new HTML("<span class=\"value_blue_s\">" + todayDateArray[0] + "</span><span class=\"body_txt_black_b\">" + todayDateArray[1] + "</span>");
					valueTable.setWidget(1, 0, todayDateHTML);
					
					List<ArrayList> dataList = (List) me.getValue();
					List<ArrayList> readingDataList = dataList.get(0);
					List<ArrayList> capacityList = dataList.get(1);
					Double panelPower = 0.0;
					Double inverterPower = 0.0;
					//FlexTable usageTable = new FlexTable();
						dataFlowValue.setWidget(0, 0,new HTML());
						dataFlowValue.setWidget(1, 1,new HTML());
						dataFlowValue.setWidget(1, 3,new HTML());
						dataFlowValue.setWidget(3, 1,new HTML());
						dataFlowValue.setWidget(2, 3,new HTML());
						dataFlowValue.setWidget(1, 2,new HTML());
					for (ArrayList dataValueList : readingDataList) {
						Date lastUpdatedDate = (Date) dataValueList.get(0);
						Double voltage = (Double) dataValueList.get(1);
						// Double current = (Double)dataValueList.get(2);
						// Double usage = (Double)dataValueList.get(3);
						String equipmentType = (String) dataValueList.get(4);
						Double power = (Double) dataValueList.get(5);

						String sPower = null;
						String sPowerUnit = null;
						if (power != null) {
							if (power < 1) {
								sPower = String.valueOf(NumberFormat.getFormat("000.000").format(1000 * power));
								sPowerUnit = "W";
							} else {
								sPower = String.valueOf(NumberFormat.getFormat("000.000").format(power));
								sPowerUnit = "KW";
							}
						}

						if (equipmentType.equals("Panel")) {
							String lastUpdateDate = DateTimeFormat.getFormat("MMM dd, yyyy hh:mm a").format(lastUpdatedDate);
							HTML lastUpdatedHTML = new HTML("<span class=\"value_blue_s\">Last updated date : </span><span class=\"body_txt_black_b\">" + lastUpdateDate + "</span>");
							dataFlowValue.setWidget(0, 0, lastUpdatedHTML);
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_LEFT);

							dataFlowValue.setWidget(1, 1, getValue(sPower, sPowerUnit));
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(1, 1, HasVerticalAlignment.ALIGN_BOTTOM);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
							panelPower = power;
						}

						if (equipmentType.equals("Utility")) {
							dataFlowValue.setWidget(1, 3, getValue(sPower, sPowerUnit));
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(1, 3, HasVerticalAlignment.ALIGN_BOTTOM);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(1, 3, HasHorizontalAlignment.ALIGN_LEFT);
						}

						if (equipmentType.equals("Battery")) {
							dataFlowValue.setWidget(3, 1, getValue(NumberFormat.getFormat("000.000").format(voltage), "V"));
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(3, 1, HasVerticalAlignment.ALIGN_MIDDLE);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(3, 1, HasHorizontalAlignment.ALIGN_RIGHT);
						}

						if (equipmentType.equals("Inverter")) {
							dataFlowValue.setWidget(2, 3, getValue(sPower, sPowerUnit));
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(2, 3, HasVerticalAlignment.ALIGN_BOTTOM);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(2, 3, HasHorizontalAlignment.ALIGN_LEFT);
							inverterPower = power;
						}
						
						if (equipmentType.equals("Diesel Generator")) {
							dataFlowValue.setWidget(1, 2, getValue(sPower, sPowerUnit));
							dataFlowValue.getFlexCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_BOTTOM);
							dataFlowValue.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_LEFT);
						}

					}
					
					for(ArrayList dataValueList : capacityList){
						String equipmentType = (String)dataValueList.get(0);
						String capacity = (String)dataValueList.get(1);
						if(equipmentType.equalsIgnoreCase("Panel")){
							dataFlowHeading.setWidget(0, 1, new HTML(capacity+" Kwp DC"));
						}
						
						if(equipmentType.equalsIgnoreCase("Inverter")){
							dataFlowHeading.setWidget(0, 3, new HTML(capacity+" Kw AC"));
						}
						
						if (inverterType.equalsIgnoreCase("Off-Grid With DG")){
							if(equipmentType.equalsIgnoreCase("Diesel Generator")){
								dataFlowHeading.setWidget(0, 3, new HTML(capacity+" kVA"));
								dataFlowHeading.setWidget(0, 2, new HTML("D G Capacity : "));
							}
						}else
							dataFlowHeading.setWidget(0, 2, new HTML("Inverter Size : "));
						if(equipmentType.equalsIgnoreCase("Battery")){
							dataFlowHeading.setWidget(0, 5, new HTML(capacity+" ah"));
						}
					}

					//dataFlowValue.setWidget(3, 0, usageTable);
					if (inverterType.equalsIgnoreCase("On-Grid")) {
						if (panelPower < inverterPower)
							dataFlowValue.setStyleName("onGrid_FromUtility");
						else
							dataFlowValue.setStyleName("onGrid_ToUtility");
					}

				}
				if(inverterType.equalsIgnoreCase("Off-Grid With DG"))
					populateRunningHours();
				populateTodayUsage();
				populateGreenHouseData();

			}
		});

	}
	
	public void cancelTimer(){
		timer.cancel();
	}
	

	private void populateTodayUsage() {
		final boolean isDGConfigured = inverterType.equalsIgnoreCase("Off-Grid With DG") ? true : false;
		ServiceUtil.getMeterServiceAsync().getTodayUsage(siteId, groupNo, isDGConfigured,new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				LoadingBox.getSharedInstance().hide();

			}

			@Override
			public void onSuccess(Object result) {
				List dataList = (List) result;
				Double solarArray = 0.0;
				Double consumed = 0.0;
				Double importEnergy = 0.0;
				Double exportEnergy = 0.0;
				if (dataList != null && dataList.size() > 0) {
					solarArray = (Double) dataList.get(0);
					consumed = (Double) dataList.get(1);
					importEnergy = (Double) dataList.get(2);
				}
				
				
//
//				if (solarArray < consumed)
//					importEnergy = consumed - solarArray;
//				else if (consumed < solarArray)
//					exportEnergy = solarArray - consumed;

				if (inverterType.contains("Off-Grid"))
					exportEnergy = 0.0;

				todayDataTable.setWidget(0, 0, getTodayTable("solar", solarArray + " kWh"));
				todayDataTable.setWidget(0, 2, getTodayTable("facility", consumed + " kWh"));
				todayDataTable.setWidget(0, 4, getTodayTable("import", importEnergy + " kWh"));
				todayDataTable.setWidget(0, 6, getTodayTable("export", exportEnergy + " kWh"));

			}
		});

	}

	private void populateGreenHouseData() {
		final boolean isDGConfigured = inverterType.equalsIgnoreCase("Off-Grid With DG") ? true : false;
		ServiceUtil.getMeterServiceAsync().getGreenHouseData(siteId, groupNo,isDGConfigured, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				LoadingBox.getSharedInstance().hide();

			}

			@Override
			public void onSuccess(Object result) {
				LoadingBox.getSharedInstance().hide();
				List dataValueList = (List) result;
				Double co2 = 0.0;
				Double so2 = 0.0;
				Double no2 = 0.0;
				if (dataValueList != null && dataValueList.size() >  2) {
					co2 = (Double) dataValueList.get(0);
					so2 = (Double) dataValueList.get(1);
					no2 = (Double) dataValueList.get(2);
				}
				StringBuffer sbValue = new StringBuffer();
				StringBuffer sbBtmValue1 = new StringBuffer();
				StringBuffer sbBtmValue2 = new StringBuffer();
				sbValue.append("<span class=\"value_black_m\">CO<sub>2</sub> :</span><br>");
				sbValue.append("<span class=\"value_blue_b\">" + co2 + "lbs</span>");

				sbBtmValue1.append("<span class=\"value_black_s\">NO<sub>2</sub> :</span><br>");
				sbBtmValue1.append("<span class=\"value_blue_m\">" + no2 + "lbs</span>");

				sbBtmValue2.append("<span class=\"value_black_s\">SO<sub>2</sub> :</span><br>");
				sbBtmValue2.append("<span class=\"value_blue_m\">" + so2 + "lbs</span>");
				FlexTable valueFlexTable = (FlexTable)greenHouseTable.getWidget(1, 0);
				valueFlexTable.setWidget(3, 0, new HTML(sbValue.toString()));
				valueFlexTable.setWidget(5, 0, new HTML(sbBtmValue1.toString()));
				valueFlexTable.setWidget(5, 2, new HTML(sbBtmValue2.toString()));

			}
		});

	}

	private void populateRunningHours() {
		ServiceUtil.getMeterServiceAsync().getEquipmentRunningHours(groupNo, new AsyncCallback() {

			@Override
			public void onFailure(Throwable caught) {
				LoadingBox.getSharedInstance().hide();

			}

			@Override
			public void onSuccess(Object result) {
				LoadingBox.getSharedInstance().hide();
				List dataList = (List) result;
				List todayRunningList = (List)dataList.get(0);
				List totalRunningList = (List)dataList.get(1);
				FlexTable valueFlexTable = (FlexTable)dgRunningTable.getWidget(1, 0);
				
				Integer todayHours = (Integer)todayRunningList.get(0);
				Integer todayMins = (Integer)todayRunningList.get(1);
				
				Integer totalHours = (Integer)totalRunningList.get(0);
				Integer totalMins = (Integer)totalRunningList.get(1);
				
				if(todayHours > 1)
					valueFlexTable.setWidget(0, 1, new HTML(todayHours+ " Hours"));
				else
					valueFlexTable.setWidget(0, 1, new HTML(todayHours+ " Hour"));
				
				if(todayMins > 1)
					valueFlexTable.setWidget(0, 2, new HTML(todayMins+ " Mins"));
				else
					valueFlexTable.setWidget(0, 2, new HTML(todayMins+ " Min"));
				
				if(totalHours > 1)
					valueFlexTable.setWidget(1, 1, new HTML(totalHours+ " Hours"));
				else
					valueFlexTable.setWidget(1, 1, new HTML(totalHours+ " Hour"));
				
				if(totalMins > 1)
					valueFlexTable.setWidget(1, 2, new HTML(totalMins+ " Mins"));
				else
					valueFlexTable.setWidget(1, 2, new HTML(totalMins+ " Min"));

			}
		});

	}

	private FlexTable getTodayTable(String subType, String value) {
		FlexTable flexTable = new FlexTable();
		Image image = null;
		HTML captionHtml = null;
		if (subType.equalsIgnoreCase("solar")) {
			image = new Image("./images/solar_panel_icon.png");
			captionHtml = new HTML("Solar array has generated : ");
		}
		if (subType.equalsIgnoreCase("facility")) {
			image = new Image("./images/house_icon.png");
			captionHtml = new HTML("Facility has consumed : ");
		}
		if (subType.equalsIgnoreCase("import")) {
			image = new Image("./images/import_icon.png");
			captionHtml = new HTML("Imported : ");
		}
		if (subType.equalsIgnoreCase("export")) {
			image = new Image("./images/export_icon.png");
			captionHtml = new HTML("Exported :");
		}
		flexTable.setWidget(0, 0, image);
		flexTable.setWidget(0, 1, captionHtml);
		flexTable.setWidget(1, 0, new HTML(value));
		flexTable.getFlexCellFormatter().setRowSpan(0, 0, 2);
		flexTable.getFlexCellFormatter().setStyleName(0, 1, "body_txt_black_b");
		flexTable.getFlexCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_BOTTOM);
		flexTable.getFlexCellFormatter().setStyleName(1, 0, "value_blue_b");

		return flexTable;
	}

	private FlexTable getValue(String value, String unit) {
		FlexTable flexTable = new FlexTable();
		flexTable.setWidget(0, 0, new HTML(value));
		flexTable.setWidget(0, 1, new HTML(unit));
		flexTable.getFlexCellFormatter().setStyleName(0, 0, "digital_txt_white");
		flexTable.getFlexCellFormatter().setStyleName(0, 1, "digital_txt_blue");
		flexTable.setStyleName("digital_disply");
		return flexTable;

	}

	private FlexTable getLiveTable(String subType, String value, String value1, String value2) {
		FlexTable mainTable = new FlexTable();
		FlexTable valueFlexTable = new FlexTable();
		Image image = null;
		Image spacerImage = new Image("./images/spacer.gif");
		HTML titleHtml = null;
		valueFlexTable.setWidget(0, 0, spacerImage);
		valueFlexTable.getFlexCellFormatter().setColSpan(0, 0, 3);
		StringBuffer sbValue = new StringBuffer();
		StringBuffer sbBtmValue1 = new StringBuffer();
		StringBuffer sbBtmValue2 = new StringBuffer();
		if (subType.equalsIgnoreCase("solar")) {
			image = new Image("./images/Sun_icon.png");
			String todayDate = DateTimeFormat.getFormat("EEEE; MMM dd, yyyy h:m a").format(new Date());
			String[] todayDateArray = todayDate.split(";");
			titleHtml = new HTML("<span class=\"value_blue_s\">" + todayDateArray[0] + "</span><span class=\"body_txt_black_b\">" + todayDateArray[1] + "</span>");
			valueFlexTable.setWidget(1, 0, titleHtml);
			valueFlexTable.getFlexCellFormatter().setColSpan(1, 0, 3);
			sbValue.append("<span class=\"value_black_m\">Solarad :</span><br>");
			sbValue.append("<span class=\"value_blue_b\">" + value + "Wft<pow>2</pow></span>");

			sbBtmValue1.append("<span class=\"value_black_s\">sunrise :</span><br>");
			sbBtmValue1.append("<span class=\"value_blue_m\">" + value1 + "</span>");

			sbBtmValue2.append("<span class=\"value_black_s\">sunset :</span><br>");
			sbBtmValue2.append("<span class=\"value_blue_m\">" + value2 + "</span>");

		}
		if (subType.equalsIgnoreCase("greenhouse")) {
			image = new Image("./images/greenhouse_icon.png");
			titleHtml = new HTML("");
			valueFlexTable.setWidget(1, 0, titleHtml);
			valueFlexTable.getFlexCellFormatter().setColSpan(1, 0, 3);
			valueFlexTable.getFlexCellFormatter().setStyleName(1, 0, "value_blue_s");
			sbValue.append("<span class=\"value_black_m\">CO<sub>2</sub> :</span><br>");
			sbValue.append("<span class=\"value_blue_b\">" + value + "lbs</span>");

			sbBtmValue1.append("<span class=\"value_black_s\">NO<sub>2</sub> :</span><br>");
			sbBtmValue1.append("<span class=\"value_blue_m\">" + value1 + "lbs</span>");

			sbBtmValue2.append("<span class=\"value_black_s\">SO<sub>2</sub> :</span><br>");
			sbBtmValue2.append("<span class=\"value_blue_m\">" + value2 + "lbs</span>");
		}

		valueFlexTable.setWidget(3, 0, new HTML(sbValue.toString()));
		valueFlexTable.getFlexCellFormatter().setColSpan(3, 0, 2);
		valueFlexTable.setWidget(2, 2, image);
		valueFlexTable.getFlexCellFormatter().setRowSpan(2, 2, 2);
		valueFlexTable.setWidget(4, 0, new HTML("<hr noshade color=\"#CCCCCC\" size=\"1\">"));
		valueFlexTable.getFlexCellFormatter().setColSpan(4, 0, 3);
		valueFlexTable.setWidget(5, 0, new HTML(sbBtmValue1.toString()));
		valueFlexTable.setWidget(5, 1, new HTML());
		valueFlexTable.setWidget(5, 2, new HTML(sbBtmValue2.toString()));
		valueFlexTable.getFlexCellFormatter().setStyleName(5, 1, "table_in");
		valueFlexTable.setStyleName("box_main_s");
		valueFlexTable.setWidth("100%");

		FlexTable headingTable = new FlexTable();
		if (subType.equalsIgnoreCase("solar"))
			headingTable.setWidget(0, 0, new HTML("Solar"));
		else
			headingTable.setWidget(0, 0, new HTML("Avoided Greenhouse Gas Emissions"));
		headingTable.getFlexCellFormatter().setStyleName(0, 0, "body_txt_blue2_b");
		headingTable.getFlexCellFormatter().setWordWrap(0, 0, true);
		headingTable.setStyleName("box_header1");
		headingTable.setWidth("100%");
		mainTable.setWidget(0, 0, headingTable);
		mainTable.setWidget(1, 0, valueFlexTable);
		mainTable.setWidth("100%");
		return mainTable;
	}
	
	private FlexTable getDGRunningTable(){
		FlexTable mainTable = new FlexTable();
		FlexTable headingTable = new FlexTable();
		FlexTable valueFlexTable = new FlexTable();
		valueFlexTable.setWidget(0, 0, new HTML("Today : "));
		valueFlexTable.setWidget(0, 1, new HTML("0 Hour"));
		valueFlexTable.setWidget(0, 2, new HTML("0 Min"));
		valueFlexTable.setWidget(1, 0, new HTML("Total : "));
		valueFlexTable.setWidget(1, 1, new HTML("0 Hour"));
		valueFlexTable.setWidget(1, 2, new HTML("0 Mins"));
		valueFlexTable.setStyleName("box_main_s");
		valueFlexTable.setWidth("100%");
		valueFlexTable.getFlexCellFormatter().setStylePrimaryName(0, 0, "value_blue_s");
		valueFlexTable.getFlexCellFormatter().setStylePrimaryName(1, 0, "value_blue_s");
		valueFlexTable.getFlexCellFormatter().setStylePrimaryName(0, 1, "body_txt_black_m");
		valueFlexTable.getFlexCellFormatter().setStylePrimaryName(1, 1, "body_txt_black_m");
		valueFlexTable.getFlexCellFormatter().setWidth(0, 0, "30%");
		valueFlexTable.getFlexCellFormatter().setWidth(0, 1, "28%");
		valueFlexTable.getFlexCellFormatter().setWidth(0, 2, "42%");
		headingTable.setWidget(0, 0, new HTML("Diesel Generator Running Hours"));	
		headingTable.getFlexCellFormatter().setStyleName(0, 0, "body_txt_blue2_b");
		headingTable.getFlexCellFormatter().setWordWrap(0, 0, true);
		headingTable.setStyleName("box_header1");
		headingTable.setWidth("100%");
		mainTable.setWidget(0, 0, headingTable);
		mainTable.setWidget(1, 0, valueFlexTable);
		mainTable.setWidth("100%");
		return mainTable;
	}
	
	public static native int getScreenWidth() /*-{
	return $wnd.screen.width;
}-*/;

public static native int getScreenHeight() /*-{
	return $wnd.screen.height;
}-*/;


}
